<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">


    <title>用户注册</title>


    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">

    <link rel="shortcut icon" href="../img/favicon.ico">

    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../plugins/ionicons/css/ionicons.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/iCheck/square/blue.css">
    <link rel="stylesheet" href="../css/zp.css">
</head>

<body>
<div class="zp-register-box">
    <div class="register-logo">
        <a href="all-admin-index.html"><b>房屋中介</b>管理系统</a>
    </div>

    <div class="zp-register-box-body">
        <p class="login-box-msg">新用户注册</p>

        <form>
            <div class="dengluming">
                <input id="loginAccount" type="text" placeholder="登录名">
                <span><img src="../img/zploginName.png" alt=""></span>
            </div>
            <div class="youjian">
                <input id="loginEmail" type="email" placeholder="Email">
                <span><img src="../img/zpEmail.png" alt=""></span>
            </div>
            <div class="mima">
                <input id="loginPassword1" type="password" placeholder="密码">
                <span><img src="../img/zpMima.png" alt=""></span>
            </div>
            <div class="queren">
                <input id="loginPassword2" type="password" placeholder="确认密码">
                <span><img src="../img/zpQueren.png" alt=""></span>
            </div>


            <div class="zp-row">
                <!-- 我同意协议 -->
                <div class="zp-row1">
                    <div class="checkbox icheck">
                        <label>
                            <input id="agreeBtn" type="checkbox"> 我同意 <a href="#">协议</a>
                        </label>
                    </div>
                </div>
                <!-- 注册 -->
                <div class="zp-row2">
                    <button id="loginRegister" type="button">注册</button>
                </div>
                <!--我有账号，现在就去登录  -->
                <div class="zp-row3">
                    <a href="index.html">我有账号，现在就去登录</a>
                </div>

            </div>
        </form>


    </div>
    <!-- /.form-box -->

</div>
<div class="slip">
    <div class="left1">
        <img src="../img/people.png" alt="">

    </div>
    <div class="left2">
        <img src="../img/bus.png" alt="">
    </div>

    <div class="right">
        <img src="../img/tree.png" alt="">
    </div>
</div>

<div class="kite">
    <div class="kite1">
        <img src="../img/kite1.png" alt="">
    </div>
    <div class="kite2">
        <img src="../img/kite2.png" alt="">
    </div>
    <div class="kite3">
        <img src="../img/kite1.png" alt="">
    </div>

</div>
<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="../plugins/iCheck/icheck.min.js"></script>
<script>
    $(function () {
        $('input').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });


        var agree = false;  //表示是否同意协议
        var registerAccount = "";   //登录名
        var registerEmail = ""; //邮箱
        var registerPassword1 = ""; //密码1
        var registerPassword2 = ""; //密码2


        //用户点击注册按钮时，注册
        $("#loginRegister").on("click", function () {
            //首先判断用户是否同意协议
            if (agree) {
                registerAccount = $("#loginAccount").val();   //登录名
                registerEmail = $("#loginEmail").val(); //邮箱
                registerPassword1 = $("#loginPassword1").val(); //密码1
                registerPassword2 = $("#loginPassword2").val(); //密码2
                //判断数据合法性
                if (isLegal()) {

                    //传输给后台，成功后，并跳转到登录页面
                    //包装成json数据
                    var dataJson = {
                        "userAccount": registerAccount,     //保存个人姓名
                        "mail": registerEmail,              //保存邮箱
                        "userPassword": registerPassword1,
                    };
                    // console.log(dataJson);
                    //ajax请求,通过post返回给后台存储
                    $.ajax({
                        url: "/user/add",   //请求路径
                        type: "post",
                        data: JSON.stringify(dataJson),
                        contentType: "application/json;charset=UTF-8",
                        dataType: "json",  //设置接收到数据邮箱
                        success: function (data) {
                            var jsonData = JSON.parse(data);
                            // alert(jsonData);
                            // console.log(typeof (jsonData));
                            if (jsonData == "no") {
                                alert("登录名已经注册！");
                            } else {
                                alert("用户注册成功！");
                                window.location.href = "/pages/index.html";
                            }

                        },    //成功
                        error: function (error) {
                            alert("注册失败！");
                            // console.log(error.responseText);
                            console.log("注册失败...");
                        }
                    })


                } else {
                    console.log("数据不合法！");
                    // alert("数据不合法！");
                }


            } else {
                alert("请同意协议！");
            }
        });

        //判断合法性
        function isLegal() {
            if (registerAccount === "") {
                alert("请输入登录名");
                return false;
            }
            if (registerEmail === "") {
                alert("请输入邮箱");
                return false;
            }
            if (registerPassword1 === "" || registerPassword2 === "") {
                alert("请填写两次密码");
                return false;
            }
            if (registerPassword1 != registerPassword2) {
                alert("确认密码填写错误，请重新填写");
                return false;
            }
            return true;
        }


        //监听选中和未选中事件
        $("#agreeBtn").on('ifChanged', function () {
            agree = true;   //表示同意
            // alert(agree);
        });

    });


</script>

</body>

</html>